package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: HouseUserController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/24 14:33
 * @SINCE 17.0.7
 * @DESCRIPTION: HouseUserController
 */
@Slf4j
@Controller
@RequestMapping("/houseUser")
public class HouseUserController extends BaseController {

    private static final String PAGE_CREATE = "houseUser/create";
    private static final String PAGE_EDIT = "houseUser/edit";
    private static final String LIST_ACTION = "redirect:/house/";
    private static final String PAGE_SUCCESS = "common/successPage";
    @Reference
    private HouseUserService houseUserService;

    @RequestMapping("/create")
    public String toCreatePage(@RequestParam("houseId") Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return PAGE_CREATE;
    }

    @RequestMapping("/save")
    public String insertHouseUser(HouseUser houseUser) {
        log.debug("houseUser = {}", houseUser);
        Integer i = houseUserService.insert(houseUser);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{id}")
    public String toEditPage(@PathVariable("id") Long id, Model model) {
        HouseUser houseUser = houseUserService.getById(id);
        model.addAttribute("houseUser", houseUser);
        return PAGE_EDIT;
    }

    @RequestMapping("/update")
    public String updateHouseUser(HouseUser houseUser) {
        Integer update = houseUserService.update(houseUser);
        return PAGE_SUCCESS;
    }

    /**
     * @param houseId 传参houseId的作用就是,删除之后可以重定向到房源详情页面
     * @param id
     * @return
     */
    @RequestMapping("/delete/{houseId}/{id}")
    public String deleteHouseUser(@PathVariable("houseId") Long houseId,
                                  @PathVariable("id") Long id) {
        Integer delete = houseUserService.delete(id);
        return LIST_ACTION + houseId;
    }
}
